package chat.ourGamePackage3;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Game extends Canvas
{
	// Bool checks
	private boolean gameRunning;
	private boolean ready;
	protected boolean leftKeyPressed;
	protected boolean rightKeyPressed;
	
	
    int FRAMES_PER_SECOND = 100;
    int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;
    int sleep_time = 0;
    long next_game_tick = GetCurrentTick();
    
	// Buffer
	private BufferStrategy strategy;
	
	// Player entity
	private Entity ship;
	
	// Hashmap containing all entity objects
	private Map<Integer,Entity> entities = new HashMap<Integer,Entity>();
	private int entityCount = 0;
	
	private String message = "";
	
	public Game()
	{
		this.gameRunning = true;
		this.ready = false;
		this.leftKeyPressed = false;
		this.rightKeyPressed = false;
		
		JFrame window = new JFrame("Game");
		JPanel panel = (JPanel) window.getContentPane();
		panel.setPreferredSize(new Dimension(800,600));
		panel.setLayout(null);
		
		setBounds(0,0,800,600);
		panel.add(this);
		
		setIgnoreRepaint(true);
		
		window.pack();
		window.setResizable(false);
		window.setVisible(true);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		addKeyListener(new KeyEvents(this));
		
		requestFocus();
		
		createBufferStrategy(2);
		strategy = getBufferStrategy();
		
		initializeEntities();
	}
	
	public void initializeEntities()
	{
		// Load entitiy objects
		ship = new ShipEntity(this, "sprites/mario.png", 370,550);
		
		// Add entities to hashmap
		this.entities.put(entityCount, ship);
	}
	
	public void gameLoop()
	{	
		BufferedImage background = null;
		while(gameRunning)
		{
			// Draw object for graphics
			Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
			/*URL url = Game.class.getResource("background.jpg");
			try
			{
				background = ImageIO.read(url);
			} catch (IOException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			g.drawImage(background, 0,0,null);
			*/
			g.setColor(Color.black);
			g.fillRect(0,0,800,600);
			
			g.setColor(Color.blue);
			g.fillRect(0, 580, 800,20);
			
			// Test draw
			g.setColor(Color.RED);
			g.drawRect(10, 10, 10, 10);
					
			//Sprite sprite = new Sprite(sourceImage);
			
			if(this.leftKeyPressed)
			{
				System.out.println("moving left");
				this.ship.moveLeft(2);
			}
			
			if(this.rightKeyPressed)
			{
				System.out.println("moving right");
				this.ship.moveRight(2);	
			}
			
			for(Map.Entry<Integer,Entity> eachEntity : entities.entrySet())
			{
				Entity e = eachEntity.getValue();
				e.Draw(g);
			}
			
			if(!ready)
			{
				g.setColor(Color.WHITE);
				g.drawString(message,(800-g.getFontMetrics().stringWidth(message))/2,250);
				g.drawString("Press any key to start the game",(800-g.getFontMetrics().stringWidth("Press any key to start the game"))/2,300);
			}
			
			// Dispose and show
			g.dispose();
			strategy.show();
			
			
			// Tick
			next_game_tick += SKIP_TICKS;
			sleep_time = (int)(next_game_tick - GetCurrentTick());
			
			if(sleep_time >= 0)
			{
				try
				{
					Thread.sleep(sleep_time);
				} 
				catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public static long GetCurrentTick()
	{
		return System.currentTimeMillis();
	}

	
	public static void main(String[] args)
	{
		Game game = new Game();
		
		game.gameLoop();
	}
}
